package com.quark.framework.database;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.quark.framework.database.QuarkDBTableInfo.QUARKDB_COLUMN_INFO;
import com.quark.framework.ormapping.QuarkOrmEntity;
import com.quark.framework.utility.QuarkLog;

public class QuarkDBMetaDataManager {
	//buffering meta data of database for querying and updating

	public String databaseName = "";
	public String databaseUser = "";
	public Map<String, QuarkDBTableInfo> tables = new HashMap<String, QuarkDBTableInfo>();	
	public QuarkDBMetaDataManager()
	{
		
	}
	
	public boolean init(DatabaseMetaData meta)
	{
		try {
			if(meta == null)
				return false;
			ResultSet rsTable = meta.getTables(null, null, null, new String[]{"TABLE"});
			boolean isPrinted = false;
			Connection conn = meta.getConnection();
			databaseUser = meta.getUserName();
			while(rsTable.next())
			{
				String tableName = rsTable.getString(3);
				if(!isPrinted)
				{
					QuarkLog.print("Gathering Information of Database " + rsTable.getString(1) + "... ");
					databaseName = rsTable.getString(1);					
					isPrinted = true;
				}
				ResultSet rsColumn = meta.getPrimaryKeys(conn.getCatalog(), null, tableName);
				while(rsColumn.next())
				{
					if(tables.get(tableName) == null)
					{
						QuarkDBTableInfo tableInfo = new QuarkDBTableInfo();
						tables.put(tableName, tableInfo);
					}
					tables.get(tableName).setProperty(rsColumn.getString(4), QUARKDB_COLUMN_INFO.COLUMN_PRIMARY_KEY, "TRUE");					
				}
				ResultSet rsImportedKey = meta.getImportedKeys(conn.getCatalog(), null, tableName);
				while(rsImportedKey.next())
				{
					if(tables.get(tableName) == null)
					{
						QuarkDBTableInfo tableInfo = new QuarkDBTableInfo();
						tables.put(tableName, tableInfo);
					}
					tables.get(tableName).setProperty(rsImportedKey.getString("FKCOLUMN_NAME"), QUARKDB_COLUMN_INFO.COLUMN_IMPORT_KEY, rsImportedKey.getString("PKCOLUMN_NAME"));
					tables.get(tableName).setProperty(rsImportedKey.getString("FKCOLUMN_NAME"), QUARKDB_COLUMN_INFO.COLUMN_IMPORT_FROM_TABLE, rsImportedKey.getString("PKTABLE_NAME"));
				}
				ResultSet rsExportedKey = meta.getExportedKeys(conn.getCatalog(), null, tableName);
				while(rsExportedKey.next())
				{
					if(tables.get(tableName) == null)
					{
						QuarkDBTableInfo tableInfo = new QuarkDBTableInfo();
						tables.put(tableName, tableInfo);
					}
					tables.get(tableName).setProperty(rsExportedKey.getString("PKCOLUMN_NAME"), QUARKDB_COLUMN_INFO.COLUMN_EXPORT_KEY, rsExportedKey.getString("FKCOLUMN_NAME"));
					tables.get(tableName).setProperty(rsExportedKey.getString("PKCOLUMN_NAME"), QUARKDB_COLUMN_INFO.COLUMN_EXPORT_TO_TABLE, rsExportedKey.getString("FKTABLE_NAME"));
				}
				
			}
			QuarkLog.println("Done");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			QuarkLog.log(e);
			return false;
		}
		return true;
	}
	
	public boolean isForeignKey(Class<? extends QuarkOrmEntity> base, String key)
	{
		QuarkDBTableInfo table = tables.get(base.getSimpleName());
		if(table == null)
			return false;
		return table.isForeignKey(key);
	}
	
	public String[] getForeignKeyInfo(Class<? extends QuarkOrmEntity> base, String key)
	{
		QuarkDBTableInfo table = tables.get(base.getSimpleName());
		if(table == null)
			return null;
		return table.getForeignKeyInfo(key);
	}
	
	public boolean isPrimaryKey(Class<? extends QuarkOrmEntity> base, String key)
	{
		QuarkDBTableInfo table = tables.get(base.getSimpleName());
		if(table == null)
			return false;
		return table.isPrimaryKey(key);		
	} 
	
}
